// pages/index/classifyList/classifyList.js
import { list } from '../../../../api/navigation'
import { getInfo } from '../../../../api/mch'
import { getInfo as getMember } from '../../../../api/member'
import { callLast } from '../../../../api/order'
const util = require('../../../../utils/util.js')
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    info: {
      type: Object
    }
  },
  lifetimes: {
    attached() {
      if (this.data.info.tagids != null) {
        this.data.tagIds = this.data.info.tagids.split(',')
        for (var i = 0; i < this.data.tagIds.length; i++) {
          this.data.tagIds[i] = parseInt(this.data.tagIds[i])
        }
      }
      this.setData({
        tagIds: this.data.tagIds
      })

      this.loadNavigation()
    }

  },
  /**
   * 组件的初始数据
   */
  data: {
    tagIds: [],
    navigations: [],
    pageSize: 8
  },

  /**
   * 组件的方法列表
   */
  methods: {
    loadNavigation() {
      list({
        mchId: app.globalData.mchId,
        pageSize: this.data.pageSize,
        tagIds: this.data.tagIds
      }).then(res => {
        this.data.navigations = res.data.data
        this.data.navigations.forEach(function (item) {
          item.logo = util.thumbnail(item.logo, 96, 96)
        })
        this.setData({
          navigations: this.data.navigations
        })
      })
    },
    toRecommend(e) {
      const { typeName, url, type, typeId } = e.currentTarget.dataset.id
      switch (type) {
        case 0:
          wx.navigateTo({
            url: '/pages/member/detail/detail?id=' + typeId,
          })
          break;
        case 1:
          wx.navigateTo({
            url: '/pages/goods/category/category?id=' + typeId
          })
          break;
        case 2:
          wx.navigateTo({
            url: '/pages/index/wxPage/wxPage?id=' + typeId
          })
          break;
        case 3:
          wx.navigateTo({
            url: '/pages/goods/product/details?id=' + typeId,
          })
          break;
        case 12:
          // 联系人工客服
          if (typeName == "tel://" || url == "tel://") this.toCall();
          // 联系在线客服
          else if (typeName == "im://" || url == "im://") wx.showToast({ title: 'im消息暂未开通~', icon: "none" });
          // 一键叫水
          else if (typeName == "quick://" || url == "quick://") {
            if (util.isNull(app.globalData.token)) {
              wx.navigateTo({ url: '/pages/login/index' });
              return
            }
            this.quickWater();
          }
          else if (typeName == "fill://" || url == "fill://") wx.navigateTo({ url: '/pages/wallet/jumpPage/saveCard/recharge' });
          /** 
           * 打开另一个小程序
           * appId，path
           **/
          else if (typeName.indexOf("mp://") == 0 || url.indexOf("mp://") == 0) {
            if ((typeName.indexOf("appId") > 0 || url.indexOf("appId") > 0) && (typeName.indexOf("path") > 0 || url.indexOf("path") > 0)) {
              let appId_index = typeName.indexOf('appId=',)
              let path_index = typeName.indexOf('&path=',)
              let appId = typeName.substring(appId_index + 6, path_index)
              let path = typeName.substring(path_index + 6)
              wx.navigateToMiniProgram({
                appId: appId,
                path: path
              })
            }
          }
          break;

        default:
          wx.showToast({
            title: '出错了~',
            icon: "none"
          })
          break;
      }
    },

    toCall() {
      var mchId = app.globalData.mchId
      getInfo(mchId).then(res => {
        if (res.type == 'success') {
          var phone = res.data.phone
          if (phone) {
            wx.makePhoneCall({
              phoneNumber: phone// 仅为示例，并非真实的电话号码
            })
          }
        }
      })
    },
    quickWater() {
      var _this = this;
      var mchId = app.globalData.mchId
      getMember(mchId).then(res => {
        if (res.type == 'success') {
          console.log(res.data, 'res.data')
          var memberId = res.data.member.id
          console.log(memberId, 'memberId', !util.isNull(memberId) && !util.isNull(mchId))
          if (!util.isNull(memberId) && !util.isNull(mchId)) {
            callLast({
              mchId: mchId,
              memberId: memberId
            }).then(result => {
              console.log(result.data, 'result.data')
              var productId = result.data.id
              wx.navigateTo({
                url: '/pages/goods/product/details?id=' + productId,
              })
            })
          }
        }
      })
    },
  }
})
